/**
 * zhanjun
 */


var id_user;
var id_name;
var dlg;
var dlgroup;
function btnAddroleClick(){
	 dlg =new J.ui.dialog({id:'addRoleDlg',title:'用户和角色关联',page:'user-role.html?idUser='+id_user+"&userName="+id_name,cover:true,width: 600, height:450,SetTopWindow:this});
	 dlg.ShowDialog();
};
function btnAddGroupClick(){
	dlgroup =new J.ui.dialog({id:'addGroupDlg',title:'用户和组关联',page:'user-group.html?idUser='+id_user+"&userName="+id_name,cover:true,width: 700, height:450,SetTopWindow:this});
	dlgroup.ShowDialog();
	
}
function delUserRole(idUser,idRole){
	if(confirm("是否确认删除选定的记录？")){
		var postdata="idUser="+idUser+"&idRole="+idRole;
		$.ajax({
			type : "POST",
			url : "../user/ser_delUserRole.do",
			data : postdata,
			success : function(msg) {
				alert(msg);
				location.reload() 			
			}
		});
	}
}
function delUserGroup(idUser,idGroup){
	if(confirm("是否确认删除选定的记录？")){
		var postdata="idUser="+idUser+"&idGroup="+idGroup;
		$.ajax({
			type : "POST",
			url : "../user/ser_delUserGroup.do",
			data : postdata,
			success : function(msg) {
				alert(msg);
				location.reload() 	
			}
		});
	}
}

$(function() {
	
	var idUser=getUrlParamValue("idUser");
	if(idUser==""){
		alert("传入的参数不正确，请重新打开！");
		location.href='user-admin.html';
	}
	var zTree1;
	
	var dlg;
	$("#idUser").attr("value",idUser);
	$.getJSON("../user/ser_loadDetails.do?id="+idUser, function(data) {
	
		$('#sp_username').html(data.userName);	
		
		id_name=data.userName;
		id_user=data.idUser;
		
		$.each(data.tbRoleSet, function(i) {
			$("<li>" + data.tbRoleSet[i].roleName + "<a href=\"javascript:delUserRole('"+idUser+"','"+data.tbRoleSet[i].idRole+"')\">[删除]</a></li>").appendTo("#user-role");
		});
		$.each(data.tbGroupSet, function(i) {
			$("<li>" + data.tbGroupSet[i].groupName + "<a href=\"javascript:delUserGroup('"+idUser+"','"+data.tbGroupSet[i].idGroup+"')\">[删除]</a></li>").appendTo("#user-group");
		});
		
		
		var zNodes1=eval(data.Privilege);	
		var setting = {
			showLine: true,
			callback : {
				click : zTreeOnClick				
			}
		};
		setting.isSimpleData = true;
		setting.treeNodeKey = "id";
		setting.treeNodeParentKey = "pId";
		zTree1 = $("#treebox").zTree(setting, zNodes1);

	});

});
function zTreeOnClick(event, treeId, treeNode) {
	
	var name = treeNode.name;
	var tid = treeId;
	var nodeid = treeNode.id;
	
	$.getJSON("../user/ser_getResOptnById.do?idUser="+id_user+"&idResource="+nodeid, function(data) {
		var trs="";
		$.each(data, function(i) {
			trs+="<tr><td>"+data[i].opetName+"</td></tr>"
			
		});
		$('#tboptnview').html(trs);	
	});
}
